<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试JavaScript</title>
</head>

<style>
    span:nth-of-type(1) {
        background-color: #f00;
    }
    span:nth-of-type(2) {
        background-color: #ff0;
    }
    span:nth-of-type(3) {
        background-color: #0f0;
    }
</style>

<body>
    <div class="box1">
        <span>span1</span>
        <span>span2</span>
        <span>span3</span>
    </div>
    <div class="main">
        <p class="box2">
            <span>span1</span>
            <span>span2</span>
            <span>span3</span>
        </p>
    </div>
</body>

<script src="js/jquery-3.2.1.min.js"></script>
<script>
    function removeSpace() {
        for (var i in arguments) {
            $(arguments[i]).contents().filter(function() {
                return this.nodeType === 3;
            }).remove();
        }
    }

    $(function() {
        removeSpace('.box1', '.main .box2')
    });
</script>

</html>